<template>
  <div>
    <el-form :model="formInline" :inline="true" class="demo-form-inline">
      <el-form-item label="单号">
        <el-input v-model="formInline.number" placeholder="请输入单据编号" clearable />
      </el-form-item>
      <el-form-item label="商品信息：">
        <el-input v-model="formInline.supName" placeholder="请输入商品信息" clearable />
      </el-form-item>
      <el-form-item label="单据日期">
        <el-date-picker v-model="formInline.date" type="datetimerange" start-placeholder="开始时间" end-placeholder="结束时间"
          format="YYYY-MM-DD HH:mm:ss" date-format="YYYY/MM/DD ddd" time-format="A hh:mm:ss"
          style="width: 280px; margin-left: 10px;" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="loadMenus">查询</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="page.records" @selection-change="handleSelectionChange" style="width: 1450px">
      <el-table-column type="selection" width="50" />
      <el-table-column prop="erpSupplier.supName" label="客户" width="140" />
      <el-table-column prop="number" label="单据编号" width="170" />
      <el-table-column prop="erpDepotItemList[0].erpMaterial.material_name" label="商品信息" width="320" />
      <el-table-column prop="createTime" label="单据日期" width="170" />
      <el-table-column prop="erpUser.username" label="操作员" width="84" />
      <el-table-column prop="erpDepotItemList[0].operNumber" label="数量" width="82" />
      <el-table-column prop="erpDepotItemList[0].allPrice" label="金额合计" width="82" />
      <el-table-column prop="erpDepotItemList[0].taxLastMoney" label="含税金额" width="82" />
      <el-table-column label="状态" width="100" align="center">
        <template #default="scope">
          <el-tag v-if="scope.row.status == 0" type="danger" effect="light">未审核</el-tag>
          <el-tag v-else-if="scope.row.status == 1" type="success" effect="light">已审核</el-tag>
          <el-tag v-else-if="scope.row.status == 2" type="warning" effect="light">完成销售</el-tag>
          <el-tag v-else-if="scope.row.status == 3" type="warning" effect="light">
            部分销售
          </el-tag>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination v-model:page-size="page.size" v-model:current-page="page.current"
      @current-change="handleCurrentChange" @size-change="handleSizeChange" :page-sizes="[2, 4, 8, 10]"
      layout="total, sizes, prev, pager, next, jumper" :total="page.total" background style="margin-top:20px;">
    </el-pagination>

    <el-button type="primary"
      @click="saveSelected({ cname: '单据明细', path: '/sale/sale_back/insert/doucumentdetailstth' }, page.records)">保存</el-button>

    <el-dialog :destroy-on-close="true" :title="title" v-model="visible" width="1550px" height="500px"
      style="margin-left: 250px;">
      <router-view @close="close" @iii="iii" :data="data"></router-view>
    </el-dialog>
  </div>
</template>

<script setup>
import { reactive, ref, defineEmits } from 'vue';
import request from '@/api/request';
import { ElMessage } from 'element-plus';
import router from '@/router';

const visible = ref(false);
const title = ref("");
let emit = defineEmits(['saveSaleinorder', 'ert', 'proty']);
const data = ref([]);

const page = ref({
  current: 1,
  size: 10,
  total: 0,
  records: []
});

const close = () => {
  visible.value = false;
};


const handleCurrentChange = (val) => {
  page.value.current = val;
  loadMenus();
};
// const handleSave = () => {
//     console.log('子组件传来的数据:', selectedItems);

// };
// handleSave();
const handleSizeChange = (val) => {
  page.value.size = val;
  page.value.current = 1;
  loadMenus();
};

const formInline = reactive({
  number: '',
  supName: '',
  date: ''
});

const loadMenus = () => {
  request({
    url: '/sale/findByPageAssociateorder',
    method: "get",
    params: {
      current: page.value.current,
      size: page.value.size,
      number: formInline.number,
      subType: '销售'
    }
  }).then(res => {
    page.value.records = res.data.records;
    page.value.total = res.data.total;
    console.log(res);
  }).catch(err => {
    ElMessage.error('加载数据失败');
  });
};

const lodelink = () => {
  console.log('loadlink');
};
const props = defineProps(["data"]);

const selectedItems = ref([])
const iii = (item) => {
  console.log(item);
  emit('ert', item);

}





const saveSelected = (p, row) => {
  if (selectedItems.value.length === 0) {
    ElMessage.warning('请至少选择一个商品');
    return;
  }
  const selectedData = selectedItems.value[0];
  title.value = p.cname;
  visible.value = true;
  router.push(p.path);
  data.value = selectedData;
  emit('proty', selectedItems.value[0]);

  console.log(selectedItems.value[0]);


};

const handleSelectionChange = (val) => {
  selectedItems.value = val;
};

const resetForm = () => {
  formInline.number = '';
  formInline.supName = '';
  formInline.date = '';
};

// 初始加载数据  
loadMenus();
</script>